import { Component, OnInit, Input } from '@angular/core';
import { FormGroup, FormBuilder } from '@angular/forms';
import { ConfigService } from './cfg.service';
import { MdlDialogService } from '@angular-mdl/core';
import { SelectItem, DynamicDialogConfig, DynamicDialogRef } from 'primeng/primeng';

@Component({
  templateUrl: './tasklist-add.component.html',
  styleUrls: ['./tasklist-add.component.scss'],
  providers: [ConfigService]
})
export class TaskListAddComponent implements OnInit {
  tasklistForm: FormGroup;
  groups: SelectItem[] = [];
  selectedGroup :any;
  tasklist: any = {};
  systemUrl: string = '';
  bizId: number = 0;
  taskListGroupId: number = 0;
  constructor(
    private fb: FormBuilder,
    private ref: DynamicDialogRef,
    private config: DynamicDialogConfig,
    private configService: ConfigService,
    private mdlDialogService: MdlDialogService) {
    this.systemUrl = config.data.systemUrl;
    this.bizId = config.data.bizId;
    this.taskListGroupId = config.data.taskListGroupId;
  }

  ngOnInit() {
    /**初始化分组选项 */
    this.configService.listTaskListGroup(this.systemUrl).subscribe(groups => {
      debugger
      if (this.taskListGroupId > 0) {
        let temp = [];
        groups.forEach(e => {
          if (e['groupId'] == this.taskListGroupId) {
            this.selectedGroup = e;
            temp.push(e);
            return;
          }
        });
        this.groups = temp;
      } else {
        this.groups = groups;
      }
    });
    this.buildForm();
  }
  /**新增表单 */
  save() {
    let tasklist = this.tasklistForm.value;

    if(null == tasklist.group){
        tasklist.group = this.selectedGroup;
    }

    if (null == tasklist.group || '' == tasklist.group) {
      this.mdlDialogService.alert('请填写或选择栏目分组');
      return;
    }
    if (tasklist.group.groupId) {
      tasklist.groupId = tasklist.group.groupId;
    } else {
      tasklist.groupName = tasklist.group;
    }
    tasklist.bizId = this.bizId;
    this.configService.addTaskList(tasklist, this.systemUrl).subscribe(data => this.ref.close(data));
  }

  /**取消关闭窗口 */
  cancel() {
    this.ref.close();
  }

  buildForm(): void {
      debugger
    this.tasklistForm = this.fb.group({
      'group': [this.tasklist.group],
      'taskListName': [this.tasklist.taskListName],
    });
    this.tasklistForm.valueChanges
      .subscribe(data => this.onValueChanged(data));
    this.onValueChanged();
  }

  onValueChanged(data?: any) {
    if (!this.tasklistForm) { return; }
    const form = this.tasklistForm;
    for (const field in this.formErrors) {
      this.formErrors[field] = '';
      const control = form.get(field);
      if (control && control.dirty && !control.valid) {
        const messages = this.validationMessages[field];
        for (const key in control.errors) {
          this.formErrors[field] += messages[key] + ' ';
        }
      }
    }
  }

  formErrors = {
    'formName': ''
  };

  validationMessages = {
    'formName': {
      'required': '栏目名称为必填项'
    }
  };

}
